import os
import json
from dbhelper import DBHelper

def main():
	mysql_connect_info = {
        'host' : '127.0.0.1',
        'port' : 3306,
        'user' : 'root',
        'password' : 'root',
        'db' : 'taobao_cat'
	}
	dbHelper = DBHelper(mysql_connect_info)

	file_path = r'./淘宝分类.txt'
	with open(file_path, 'r') as f:
		cat_info = json.loads(f.read())
		item_cat = cat_info['item_cats']['item_cat']
		# print(len(item_cat))
		index = 1
		for item in item_cat:
			process_item_cat(item, dbHelper, index)
			index = index + 1

def process_item_cat(item, dbHelper, index):
	sql = "insert into taobao_cat_info(cid, is_parent, name, parent_cid) values (%s, %s, %s, %s)"
	cid = item['cid']
	is_parent = 1 if item['is_parent'] else 0
	name = item['name']
	parent_cid = item['parent_cid']
	params = (cid, is_parent, name, parent_cid)
	dbHelper.insert(sql, *params)
	print("insert success:%d"%index)



if __name__ == '__main__':
	main()